/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/29
 */

import Vue from 'vue';
import tpl from './tpl.html';

import filter from '../../../../grsc-base/util/filter';
import CONFIG from './config';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Pager from '../../../../grsc-base/component/Pager.vue';
import {PAGE_SIZE_5} from '../../../../grsc-base/common/constants';
import service from './service';

export default Vue.extend({
    // name: 'myorder',
    template: tpl,
    data(){
        return {
            isLoading: true,
            CONFIG: CONFIG,

            orderList: [],
            pageNum: 0,
            condition: {
                pageNo: 1,
                pageSize: PAGE_SIZE_5
            },

            orderType: '', // 當前訂單是買單還是買單
            status: '', // 當前訂單的狀態 未支付 已支付 已完成 申訴中

            currentOrder: {}, // 當前 確認支付 或 確認打幣 訂單
            showDialog: false, // 資金密碼輸入框
        }
    },
    created() {
        // 申訴返回
        let source = this.$route.query.status || '';
        if (source === CONFIG.TRADE_STATUS_COMPLAINNING.KEY) {
            this.status = source;
        }
        this.fetchOrderList();
    },
    mounted(){

    },
    components: {
        Loading,
        NoData,
        Pager
    },
    methods: {
        changeTab(_param){
            this.status = _param;
        },

        fetchOrderList(){
            this.isLoading = true;

            service.orderQuery({
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize,
                orderType: this.orderType,
                status: this.status
            }).done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    let listArr = rep.data.list;
                    listArr.forEach((item, index) => {
                        switch (item.status) {
                            case 'FINISHED':
                                item.status = '交易完成';
                                break;
                            case 'CANCELED':
                                item.status = '交易取消';
                                break;
                            case 'CLOSED':
                                item.status = '交易關閉';
                                break;
                            case 'OVERTIME':
                                item.status = '交易超時';
                                break;
                            default:
                                break;
                        }

                        if (!item.remark) {
                            item.remark = '無';
                        }

                        // 買單倒計時
                        if (this.orderType === 'BUY' && this.status === 'UNPAY') {
                            item.leftDate = 1800000 - (parseInt(item.currentDate) - parseInt(item.createDate));
                            if (item.leftDate <= 0) {
                                item.leftDate = 0;
                                return;
                            }
                            item.timer = setInterval(function () {
                                item.leftDate = item.leftDate - 1000;
                                if (item.leftDate <= 1000) {
                                    item.leftDate = 0;
                                    window.location.reload();
                                }
                            }, 1000)
                        }

                    });
                    this.orderList = listArr;
                    this.pageNum = Number(filter.ceilFix(rep.data.total / this.condition.pageSize, 0));
                } else if (rep.code === window.CODE.NO_PERMISSION.CODE) {
                    window.error('未登錄', () => {
                        window.location.href = './login.html';
                    }, 2000)
                } else {
                    window.error(rep.msg);
                }
            }).always(rep => {
                this.isLoading = false;
            })
        },

        /**
         * 分页
         * @param pageNo
         */
        changePage(pageNo) {
            this.condition.pageNo = pageNo;
            this.fetchOrderList();
        },

        /**
         * 取消訂單
         * @param transOrderId
         */
        cancelOrder(item, $event){
            window.confirm({
                target: $event.target,
                content: `您每天可取消3次訂單，取消訂單后，您將無法正常完成${item.assetCode}的購買，確定要取消嗎？`,
                confirmBtnText: '確定',
                cancelBtnText: '取消',
            }).done(() => {
                service.buyOrderCancel({
                    transOrderId: item.transOrderId
                }).done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        this.fetchOrderList();
                    } else {
                        window.error(rep.msg);
                    }
                })
            }).fail(() => {

            });
        },

        /**
         * 確認支付
         * @param transOrderId
         */
        confirmPay(item){
            this.showDialog = true;
            this.currentOrder = item;
        },

        /**
         * 確認打幣
         * @param transOrderId
         */
        confirmTransfer(item){
            this.showDialog = true;
            this.currentOrder = item;
        },

        hideDialog(){
            this.showDialog = false;
            this.fetchOrderList()
        },

        changePage(currentPage) {
            this.condition.pageNo = currentPage;
            this.fetchOrderList();
        },

        /**
         * 申訴
         * @param pageNo
         */
        toComplain(_id, _type){
            this.$router.push({name: 'complain', params: {transOrderId: _id}, query: {source: _type}})
        }
    }
});

